package com.example.helper2;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import java.util.LinkedList;
import java.util.List;

public class HomeCardAdapter extends BaseAdapter {
    // 列表所有数据
    private List<HomeCardInfo> mlist;
    // 获取当前上下文
    private Context mcontext;

    public HomeCardAdapter(List<HomeCardInfo> mlist, Context mcontext){
        this.mlist = mlist;
        this.mcontext = mcontext;
    }

    public int getCount(){
        return mlist.size();
    }

    public Object getItem(int position){
        return mlist.get(position);
    }
    public long getItemId(int position){
        return position;
    }

    public View getView(final int position, View convertView, ViewGroup parent){
        viewholder vh = null;
        if (convertView == null){
            vh = new viewholder();
            convertView = LayoutInflater.from(mcontext).inflate(R.layout.home_list_card,null);
            vh.mCardType = (TextView)convertView.findViewById(R.id.card_type);
            vh.mCardTitle = (TextView)convertView.findViewById(R.id.card_title);
            vh.mCardContent = (TextView)convertView.findViewById(R.id.card_content);
            vh.mbutton = (Button)convertView.findViewById(R.id.more);
            convertView.setTag(vh);
        }else{
            vh = (viewholder)convertView.getTag();
        }
        vh.mCardType.setText("[ "+mlist.get(position).getType()+" ]");
        vh.mCardTitle.setText(mlist.get(position).getTitle());
        if(mlist.get(position).getContent().length() > 50){
            String contentTemp = mlist.get(position).getContent().substring(0,51) + "...";
            mlist.get(position).setContent(contentTemp);
        }
        vh.mCardContent.setText(mlist.get(position).getContent());
        vh.mbutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnItemShowListener.onShowClick(position);
            }
        });
        return convertView;
    }

    // 监听按钮点击事件接口，展示对话框
    public interface onItemShowDialogListener{
        void onShowClick(int position);
    }
    // 自定义监听事件接口
    private onItemShowDialogListener mOnItemShowListener;
    // 自定义监听事件
    public void setOnItemShowDialogClickListener(onItemShowDialogListener mOnItemShowListener){
        this.mOnItemShowListener = mOnItemShowListener;
    }

    public class viewholder{
        TextView mCardType;
        TextView mCardTitle;
        TextView mCardContent;
        Button mbutton;
    }
}
